/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.data;

import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 *
 * @author home
 */
public class ManagerBase<T> extends HibernateUtil {

    private Class<T> persistentClass;

    public ManagerBase() throws Exception {
        super();
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public ManagerBase(String sessionFactotyConfigPath) {
        super(sessionFactotyConfigPath);
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    public Class<T> getPersistentClass() {
        return this.persistentClass;
    }

    /**
     * Add new entity and commit it to database
     * @param entity
     * @throws Exception
     */
    public boolean addNew(T entity) throws Exception {        
        beginTransaction();
        getCurrentSession().save(entity);
        commitAndClose();
        return true;
    }

    /**
     * Update or Add a entity and commit it to Database
     * @param entity
     * @throws Exception
     */
    public boolean update(T entity) throws Exception {
        beginTransaction();
        getCurrentSession().saveOrUpdate(entity);
        commitAndClose();
        return true;
    }

    /**
     * Delete a entity and commit action to Database
     * @param entity
     * @throws Exception
     */
    public boolean delete(T entity) throws Exception {
        beginTransaction();
        getCurrentSession().delete(entity);
        commitAndClose();
        return true;
    }

    /**
     * Get all rows in table of entity
     * @return
     * @throws Exception
     */
    public List<T> getAll() throws Exception {
        List<T> list = null;

        beginTransaction();
        list = getCurrentSession().createCriteria(this.persistentClass).list();
        commitAndClose();

        return list;
    }

    /**
     * Get a entity by Id
     * @param id of entity
     * @param locked lock entity
     * @return a entity
     * @throws Exception
     */
    @SuppressWarnings("static-access")
    public T getById(int id, boolean locked) throws Exception {
        T entity;

        beginTransaction();

        if (locked) {
            entity = (T) getCurrentSession().load(getPersistentClass(), id, lockMode.UPGRADE);
        } else {
            entity = (T) getCurrentSession().load(getPersistentClass(), id);
        }

        commitAndClose();

        return entity;
    }

    /**
     * Execute a sql non query
     * @param sql non query
     * @return number of row effected
     * @throws Exception
     */
    public int excecuteSQl(String sql) throws Exception {
        int i = 0;

        beginTransaction();
        i = getCurrentSession().createSQLQuery(sql).executeUpdate();
        commitAndClose();

        return i;
    }
}
